package ru.study.jdbc.ext;

import java.lang.reflect.*;
import java.sql.PreparedStatement;

import ru.study.jdbc.entity.AbstractEntity;

public class OrmUtils {
	public static <T extends AbstractEntity> PreparedStatement setParamByValue(T entity,
			String fieldname, PreparedStatement prestmt, int i) {
		Class cls = entity.getClass();

		try {
			Field f = cls.getDeclaredField(fieldname);
			f.setAccessible(true);
			Class<?> typeclass = f.getType();
			Object value = f.get(entity);			
			
			if (typeclass == String.class) {
				prestmt.setString(i, (String) value);
				System.out.println("string value="+value);
			} else if (typeclass == Integer.class) {
				prestmt.setInt(i, (Integer) value);
				System.out.println("int value= "+value);
			} else if (typeclass == Long.class) {
				prestmt.setLong(i, (Long) value);
				System.out.println("long value= "+value);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return prestmt;
	}
}
